/* 
 * File:   NetworkManager.h
 * Author: noe
 *
 * Created on 7 octobre 2013, 19:11
 */

#ifndef NETWORKMANAGER_H
#define	NETWORKMANAGER_H

#include <iostream>
#include <vector>
#include <thread>
#include <string>

#include <netinet/in.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)

typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
  

#define PORT 8080

class NetworkManager {
public:
	NetworkManager();
	NetworkManager(const NetworkManager& orig);
	virtual ~NetworkManager();
	
	/**
	 * 
     * @param server adress
     * @return true if connected
     */
	bool connectTo(std::string adress);
	
	/**
	 * 
     * @param string to send
     * @return true if sent
     */
	bool sendMessage(std::string msg);
	
	/**
	 * 
     * @return messages got until now
     */
	std::vector<std::string> getMessages();

	/**
	 * ends the connection with the server
     * @return whether the connection has been closed
     */
	bool close();
	
private:
	
	/**
	 * the server listening loop, in a separate thread
     */
	void* readLoop();
		
	std::vector<std::string> messages;
	std::vector<std::string> msgcpy;
	char buffer[32];
		
	SOCKET sock;
	SOCKADDR_IN sin;

};

#endif	/* NETWORKMANAGER_H */

